import { checkLogin, getTagData, userIdParam } from "../../utils/util";

// pages/groups/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        color: wx.$theme.color
    },
    tapPlace: function(e) {
        const {id, pname} = getTagData(e)
        wx.setStorageSync('place', {placeId: id, pname})
        wx.navigateTo({
          url: `./room/room?placeId=${id}&pname=${pname}`,
        })
    },

    tapAddPlace: function() {
        wx.navigateTo({
          url: './addPlace/addPlace',
        })
    },

    tapUpdatePlace: function(e) {
        const {id, pname} = getTagData(e);
        wx.navigateTo({
          url: `./addPlace/addPlace?id=${id}&pname=${pname}`,
        })
    },

    tapDeletePlace: function(e) {
      const {id, pname} = getTagData(e);
      wx.showModal({
        title: '提示',
        content: `确定删除${pname}场所？`,
        confirmColor: wx.$theme.color,
        complete: (res) => {
          if (res.confirm) {
              wx.$http.delete({url: '/szm/place/', data: [id]}).then(()=> {
                  wx.showToast({
                    title: '删除场所',
                  });
                  this.init();
              });
          }
        }
      })
  },

    bindPageList: function(e) {
        const {list, page} = e.detail;
        const {pList} = this.data;
        this.setData({pList: page === 1 ? list : pList.concat(list)});
    },

    init() {
        this.page_this.data.page = 1;
        this.page_this.data.limit = 1000;
        this.page_this.getPageData({url: '/szm/place/page', data: userIdParam()});
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({pList: []})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.page_this = this.selectComponent('#page')
        if(checkLogin("no")) {
            this.init();
            this.setData({isLogin: true});
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    tapLogin: function() {
        wx.navigateTo({
          url: '../login/index',
        })
      },
})